#coding=utf-8

'''
Created on 2011-8-29
采集数据的实体模板
@author: chris
'''
from ripper.parser.TTCNParser import TTCNParser
from ripper.core.Exceptions import RipperException
from ripper.core.Storage import db
from ripper.core.Page import Page
from ripper.core.Property import Property
from ripper.parser.AisexParser import AisexParser
from ripper.parser.UHentaiParser import UHentaiParser
from ripper.parser.HentaiSchoolParser import HentaiSchoolParser
from ripper.parser.SisParser import SisParser

def get_item_by_name(itemName):
    item = db.query_one('item', {'name' : itemName})
    if item == None :
        raise RipperException('Invalid Item Name : ' + itemName)
    return Item(item)

class Item(object):
    '''
    采集数据的实体模板类 
    '''
    
    def __init__(self, item):
        # 加载item内容
        self.__item = item
        self.pages = []
        self.properties = []
        self.parsers = {}
        
    # 调用解析器
    def get_parser(self):
        
        # 解析器单例返回
        
        if self.parsers.has_key(self.__item['name']) == False:
        
            if self.__item['name'] == 'tabletennisdb':
                self.parsers[self.__item['name']] = TTCNParser(self)
            
            elif 'aisex' in self.__item['name']:
                self.parsers[self.__item['name']] = AisexParser(self, needProxy=True)
            
            elif 'uhentai' == self.__item['name']:
                self.parsers[self.__item['name']] = UHentaiParser(self, needProxy=False)
            
            elif 'hhentai' == self.__item['name']:
                self.parsers[self.__item['name']] = HentaiSchoolParser(self, needProxy=False)
            
            elif 'sis_' in self.__item['name']:
                self.parsers[self.__item['name']] = SisParser(self, needProxy=True)
            
            else :
                
                raise RipperException(u'不支持的解析器类型:' + self.name) 
            
        
        return self.parsers[self.__item['name']]
    
    
    
    #获得采集起始页
    def get_index_page(self):pass
    
    # 读取property对象
    def get_properties(self, raw=False):
        properties = self.__item['properties']
        self.properties = []
        for p in properties :
            misc = None
            if p.has_key('misc'):
                misc = p['misc']
            prop = Property(p, misc=misc)
            if raw == True:
                self.properties.append(p)
            else:
                self.properties.append(prop)
        return self.properties
    
    # 读取单个配置属性值
    def get_property(self, name):
        return self.__item[name]
    
    # 读取页面对象
    def get_pages(self):
        if self.pages != []:
            return self.pages
        pages = self.__item['pages']
        for p in pages :
            self.pages.append(Page(p['name'], p['desc'], p['url']))
        return self.pages
    
    # 获得采集唯一标识
    def get_key_property(self):
        for p in self.__item['properties']:
            if p.has_key('misc'):
                p = Property(p, p['misc'])
            else:
                p = Property(p)
            if p.is_key() == True:
                return p
        raise RipperException(u'key property 未配置')